package com.ukao.cashregister.adapter;

import android.content.Context;
import android.view.View;

import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mcxtzhang.commonadapter.rv.CommonAdapter;
import com.mcxtzhang.commonadapter.rv.ViewHolder;
import com.ukao.cashregister.R;
import com.ukao.cashregister.bean.ClothIngManageBean;
import com.ukao.cashregister.listener.OnItemChildClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.MyDateUtils;
import com.ukao.cashregister.widget.StateButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by ukaodev on 2017/6/14.
 */

public class ClothingManageAdapter extends CommonAdapter<ClothIngManageBean.ListBean> {





    public ClothingManageAdapter(Context context, List<ClothIngManageBean.ListBean> datas) {
        super(context, datas, R.layout.adapter_clothing_manage);
    }

    private OnItemChildClickListener mOnItemChildClickListener;

    public void setOnItemChildClickListener(OnItemChildClickListener mOnItemChildClickListener) {
        this.mOnItemChildClickListener = mOnItemChildClickListener;
    }


    @Override
    public void convert(final ViewHolder viewHolder, ClothIngManageBean.ListBean item) {
        StateButton markingBtn=viewHolder.getView(R.id.marking_btn);
        RelativeLayout orderNoLayout=viewHolder.getView(R.id.orderNo_layout);
        StateButton bindMarkBtn=viewHolder.getView(R.id.bind_mark_btn);
        TextView tvBindCode =viewHolder.getView(R.id.tv_bind_code);
        viewHolder.setText(R.id.tranTypeText,CheckUtils.isEmptyString(item.getTranTypeText()));
        viewHolder.setText(R.id.factoryName,CheckUtils.isEmptyString(item.getFactoryName()));
        viewHolder.setText(R.id.tranNum,CheckUtils.isEmptyString(item.getTranNum()));
        viewHolder.setText(R.id.productName,item.getProductName() );
        viewHolder.setText(R.id.orderNo,item.getOrderNo() );
        viewHolder.setText(R.id.statusText,item.getStatusText() );
        viewHolder.setText(R.id.subtotal,"小计：¥"+CheckUtils.isEmptyNumber(item.getSubtotal()) );
        viewHolder.setText(R.id.scanCode,"条码："+item.getScanCode() );
        viewHolder.setText(R.id.createTime,"入库时间："+ MyDateUtils.formatDataTimes(item.getCreateTime()));
        viewHolder.setText(R.id.batchInCode,CheckUtils.isEmpty(item.getBatchInCode())?"":"入厂袋号："+item.getBatchInCode());
        viewHolder.setText(R.id.batchOutCode,CheckUtils.isEmpty(item.getBatchOutCode())?"":"出厂批次："+item.getBatchOutCode());
        tvBindCode.setText(item.getBindCode());
        if(item.isEnableBindCode()){ //为true 显示
            bindMarkBtn.setVisibility(View.VISIBLE);
        }else {  //为false隐藏
            bindMarkBtn.setVisibility(View.INVISIBLE);
        }
        if(item.getStatusVal()==51){  //等于51  就是已签收
            viewHolder.setTextColor(R.id.statusText, mContext.getResources().getColor(R.color.green));
        }else {
            viewHolder.setTextColor(R.id.statusText,mContext.getResources().getColor(R.color.warm_red));
        }
        if (CheckUtils.isEmpty(item.getBindCode())){  //为null ""
            bindMarkBtn.setText(R.string.tie_the);
            bindMarkBtn.setNormalBackgroundColor(mContext.getResources().getColor(R.color.blue));
            bindMarkBtn.setPressedBackgroundColor(mContext.getResources().getColor(R.color.default_blue));
        }else{   //不为null ""
            bindMarkBtn.setText(R.string.has_been_tied);
            bindMarkBtn.setNormalBackgroundColor(mContext.getResources().getColor(R.color.green));
            bindMarkBtn.setPressedBackgroundColor(mContext.getResources().getColor(R.color.un_green));
        }


        viewHolder.setText(R.id.scanCode,"条码："+item.getScanCode() );
        markingBtn.setVisibility(item.isEnablePrint() ?View.VISIBLE :View.GONE);
        String   userJson=item.getUserJson();
        try {
            if (!CheckUtils.isEmpty(userJson)){
                JSONObject    mJSONObject=new JSONObject(userJson);
                String  name=mJSONObject.getString("n");
                String  phone=mJSONObject.getString("p");
                viewHolder.setText(R.id.userJson,name+"  "+phone);
                item.setPhone(phone);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }



        View.OnClickListener OnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CheckUtils.isNull(mOnItemChildClickListener))
                    mOnItemChildClickListener.onChildItemClick(v.getId(), getPosition(viewHolder));
            }

        };

        orderNoLayout.setOnClickListener(OnClickListener);
        markingBtn.setOnClickListener(OnClickListener);
        bindMarkBtn.setOnClickListener(OnClickListener);
    }


}
